<?php

/**
 * IndexController - The default controller class
 * 
 * @author
 * @version 
 */

class Adminb_MlmController extends App_Zend_Controller_Action_Adminb
{
	

	public function init(){
		parent::init();
		$member=new CMember();
		
		if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName())){

			
				$this->_redirect(MY_URL.'/auth-login?preurl='.CURRENT_URL);

			
		}
	}
	
	public function preDispatch()
	{

		$this->getHelper ('layout')->setLayout('framelayout');
		
		$this->view->addHelperPath(MY_ROOT.'/vhelpers');
		
		Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');
		
		$this->view->assign('currentAction',$this->_request->getActionName());
			
	}

    public function indexAction() 
    {
    	$mlm = new CMlm();
    	$uid = $_COOKIE[COOKIE_PRE . 'uid'];

		//是否已经购买过商品
		$hasBuy = $mlm->hasSuccessBuy($uid);
		$this->view->hasBuy = $hasBuy;

		//账户总额
		$this->view->accountAmount = $mlm->getAllCash($uid);
		
		//申请成功的提款额
		$this->view->successApplyAmount = $mlm->getSuccessCash($uid);
		
		//申请中的提款额
		$this->view->applyingAmount = $mlm->getInProcessCash($uid);
		
		//申请退回的提款额
		$this->view->failApplyAmount = $mlm->getRefuseCash($uid);
		
		if ($hasBuy) {
			$inviteCode = $mlm->memberInviteCode($uid);
			//营销链接
			$this->view->marketingLink = MY_URL.'/bring-a-friend-and-earn-with-us/' . $inviteCode;
			//邀请码，邀请链接实际上包含了邀请码
			$this->view->inviteCode = $inviteCode;
		}
		
		$level1Uids = $mlm->getLevel1Uid($uid);
		//一级用户数量
		$this->view->level1UserCount = count($level1Uids);
		
		//一级用户订单总额
		$this->view->level1UserAmount = $mlm->getOrderAmountByUid($level1Uids);
		
		$level2Uids = $mlm->getLevel2Uid($uid);
		//二级用户数量
		$this->view->level2UserCount = count($level2Uids);
		
		//二级用户订单总额
		$this->view->level2UserAmount = $mlm->getOrderAmountByUid($level2Uids);
		
		$level3Uids = $mlm->getLevel3Uid($uid);
		//三级用户数量
		$this->view->level3UserCount = count($level3Uids);
		
		//三级用户订单总额
		$this->view->level3UserAmount = $mlm->getOrderAmountByUid($level3Uids);

    }
    
    
    public function paypalAction(){
    	
    }
    
    public function dopaypalAction(){
    	
    	$this->_helper->viewRenderer->setNoRender();

    	$paramArray=$this->_request->getParams();
    	$filters = array(
    		'*'   => 'StringTrim'
		);

		//判断是否为空，paypal帐号是否为email，amount是否为数字
		$validators = array(
		
		);
		$input = new Zend_Filter_Input($filters, $validators, $paramArray);
		$paypalAccount=$input->paypalAccount;
		$amount=$input->amount;
		
		$validator = new Zend_Validate_EmailAddress();
		$error = '';
		if (!$validator->isValid($paypalAccount)) {
			$error .= "Paypal Account must be en email. ";
		}
		
		$validator = new Zend_Validate_Float();
    	if (!$validator->isValid($amount)) {
			$error .= "The ammount you input is invalid";
		}		
		
		if($error){
			$this->view->showMsg($error);
            return false;
		}
		
		$mlm = new CMlm();
		$uid = isset($_COOKIE[COOKIE_PRE.'uid'])?$_COOKIE[COOKIE_PRE.'uid']:0;
		if ($mlm->cash($uid, $amount, $paypalAccount)) {
			$this->view->showMsg('Applying...','adminb-mlm-index');
		} else {
			$this->view->showMsg('No available amount','adminb-mlm-index');
		}
    }
    
    public function paypalrecordAction()
    {
		$cashModel = new CMlmcashlog();
		$page = $this->getRequest()->getParam('page', 1);
		$pageNum = 10;
		
		$type = $this->getRequest()->getParam('type', 5);
		
		$type = intval($type);
		
		$uid = isset($_COOKIE[COOKIE_PRE.'uid'])?$_COOKIE[COOKIE_PRE.'uid']:0;
		$cashList = $cashModel->getAll($page, $pageNum, $type, $uid);
		$pageCount = $cashModel->getPageCount($pageNum, $type, $uid);
		$statistics = new CStatistics();        
        
        $param = 'adminb-mlm-paypalrecord?type=' . $type . '&';
        $pageLink = CPage::divPagesAdmin(ceil($pageCount/$pageNum), $page, $param);
        
        $this->view->pageLink = $pageLink;
        $this->view->cashList = $cashList;
        $this->view->pageCount = $pageCount;
        $this->view->type = $type;    	
    }
    
    public function exchangeAction()
    {
    	$uid = isset($_COOKIE[COOKIE_PRE.'uid'])?$_COOKIE[COOKIE_PRE.'uid']:0;
    	
    	$mlm = new CMlm();
    	$this->view->accountAmount = $mlm->getAllCash($uid);
    	
    	$config = new CConfig();
    	$score = $config->getByName('ORDER_SCORE');
    	$scoreArr = explode('|', $score->data);
    	$this->view->score = $scoreArr[0];
    }
    
	public function doexchangeAction(){
    	
    	$this->_helper->viewRenderer->setNoRender();

    	$paramArray=$this->_request->getParams();
    	$filters = array(
    		'*'   => 'StringTrim'
		);

		//判断是否为空，paypal帐号是否为email，amount是否为数字
		$validators = array(
		
		);
		$input = new Zend_Filter_Input($filters, $validators, $paramArray);
		
		$amount=$input->amount;
		
		$validator = new Zend_Validate_EmailAddress();
		$error = '';		
		
		$validator = new Zend_Validate_Float();
    	if (!$validator->isValid($amount)) {
			$error .= "The ammount you input is invalid";
		}		
		
		if($error){
			$this->view->showMsg($error);
            return false;
		}
		
		$mlm = new CMlm();
		$uid = isset($_COOKIE[COOKIE_PRE.'uid'])?$_COOKIE[COOKIE_PRE.'uid']:0;
		if ($mlm->exchange($uid, $amount)) {
			$this->view->showMsg('Success.','adminb-account-index');
		} else {
			$this->view->showMsg('No available amount','adminb-mlm-exchange');
		}
    }

    
	public function postAction(){
	
		//$this->_helper->viewRenderer->setNoRender();
		
		//$this->getHelper ('layout')->disableLayout ();
	
	}
    

    
}
